/*
 * 创建时间：17-9-22
 * 作者：sce
 * 功能描述：ajax的promise封装
 * 修改时间：
 */
import axios from 'axios'
import md5 from 'js-md5'

class PromisePost{
  constructor(){
    this.parameter = {};
  }
  get uri(){
    return this.parameter.uri;
  }
  set uri(value){
    this.parameter.uri = value;
  }
  get method(){
    return this.parameter.method;
  }
  set method(value){
    this.parameter.method = value;
  }
  get options(){
    return this.parameter.options;
  }
  set options(value){
    this.parameter.options = value;
  }
  get header(){
    return this.parameter.header
  }
  set header(value){
    this.parameter.header = value;
  }
  getFormatDate(){
    let date = new Date();
    let time = {
      year:date.getFullYear(),
      mouth:date.getMonth()+1,
      day:date.getDate(),
      hour:date.getHours(),
      min:date.getMinutes(),
      sec:date.getSeconds(),
    }
    for(let i in time){
      if(time[i]>=0&&time[i]<10){
        time[i] = "0" + time[i];
      }
    }
    let format = "yyyy-MM-dd hh:mm:ss";
    format =  format.replace(/yyyy/g,time.year).replace(/MM/g,time.mouth).replace(/dd/g,time.day).replace(/hh/g,time.hour).replace(/mm/g,time.min).replace(/ss/g,time.sec);
    return format;
  }
  sortObjectKey(obj){
  let keyArr = Object.keys(obj).sort();
  let sorted = new Object();
  for(let i in keyArr){
    sorted[keyArr[i]] = obj[keyArr[i]];
  }
  return sorted;
  }
  createDesign(){
    let obj = new Object();
    obj.method = this.method;
    obj.timestamp = this.getFormatDate();
    obj.app_key = "H9vUQm8nmwn9uNmg2ibRDp0BRm32R2qS";
    obj.version = "1.0";
    obj.sign_method = "md5";
    // obj.sign = "";
    for(let i in this.options){
      if(this.options[i]!=undefined)
      {
        obj[i] = this.options[i];
      }
    }
    let sorted = this.sortObjectKey(obj);
    let str = ""
    let secret = "CAE3AA84D983B94E25B63D8D5CA766B1"
    for(let i in sorted){
      str += i + sorted[i];
    }
    str = secret + str + secret;
    str = md5(str).toUpperCase();
    obj.sign = str;

    this.parameter.options = obj;
  }
  createXHR(){
    if(window.XMLHttpRequest){
      return new XMLHttpRequest();
    }
    return new ActiveXObject('Microsoft.XMLHTTP');
  }
  ajaxPromise(){
    return axios.post(this.uri, this.options, {withCredentials:true})
    .then(function(response) {
      return response.data;
    })
  //   return new Promise((resolve, reject) => {
  //     let xhr = this.createXHR();
  //     xhr.open("POST", this.uri, true);
  //     xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  //     xhr.onreadystatechange = function () {
  //       if (this.readyState === 4) {
  //         if (this.status === 200) {
  //           let resolveJSON = JSON.parse(this.responseText);
  //           resolve(resolveJSON)
  //         } else {
  //           let resJson = { status: this.status, response: this.response};
  //           reject(resJson)
  //         }
  //       }
  //     }
  //     xhr.send(JSON.stringify(this.options));
  //   })
  }
}
export default PromisePost;
